package net.xiduth.tools.security;


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//Using SQL later on
/**
 * @author Kyle(Googlerapesbing)
 *
 */
public class Security extends JFrame implements ActionListener {
	/**
	 * @serial creates the generated number
	 * @TextField creates a field for the user
	 * @PasswordField creates a field for the user's password
	 * @Labels states the entrance of either the user or password
	 * @Button creates an ActionListener to login if it has the correct information
	 */
	private static final long serialVersionUID = 2573208763127585548L;
	JTextField userField;
	JPasswordField passField;
	JLabel username;
	JLabel password;
	JButton login;
	JLabel status;
	private Object loginpass;//temp
	private Object name;//temp
	ImageIcon image;
	/*
	 * @Security creates location and sets the x and y to set the fields
	 * (TextField,PasswordField,Labels, etc.)
	 */
	public Security(){
		userField = new JTextField(10);
		passField = new JPasswordField(10);
		username = new JLabel("Username:");
		password = new JLabel("Password:");
		login = new JButton("Login");
		status = new JLabel("Status:");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*Sets the close operation*/
		setSize(600, 500);/*Sets the Size of the window*/
		setResizable(true);/*Resizable or not*/
		
		BorderLayout pic = new BorderLayout();
		pic.addLayoutComponent(new JLabel(), BorderLayout.CENTER);
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setLayout(grid);
		c.gridx = 0;
		c.gridy = 0;
		add(username,c);/*Adds the username name label*/
		c.gridx = 0;
		c.gridy = 1;
		add(password,c);/*Adds the password label*/
		c.gridx = 1;
		c.gridy = 2;
		add(login, c);/*Login button*/
		c.gridx = 0;
		c.gridy = 3;
		add(status,c);/*Sends a message*/
		c.gridx = 1;
		c.gridy = 0;
		add(userField,c);/*Entering user name*/
		c.gridx = 1;
		c.gridy = 1;
		add(passField,c);/*Entering password*/
		login.addActionListener(this);/*Adds THIS listener*/
		setVisible(true);
	}
	
	/*
	 * @Main runs Security
	 */
	public static void main(String[] args){
		
		@SuppressWarnings("unused")
		Security l = new Security();		
	}
	
	/*
	 *@Performs the login action
	 */
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == login){
			String user = userField.getText();
			String pass = passField.getText();
			if(user.equals(name) && pass.equals(loginpass)){
				status.setText("Status:Logged in");
				
				try {/* @Writes inside the set location(have to make the boolean true or it will not write a new line) */
					BufferedWriter fw = new BufferedWriter(new FileWriter("C:\\Users\\Kyle\\Desktop\\writer.txt", true));
					fw.write(user + "  " + pass);/*@Writes what you want it to write*/
					fw.newLine();/*@Creates a new line */
					fw.flush();/*@Flushes it*/
					fw.close();/*@Closes it*/
				} catch (IOException ee) {
				/* @Catchs if needed */
					
				}
			}else if(!user.equals(name)){/*@If not equal to the string(user) then sends a message*/
				status.setText("Status:You entered the wrong username.");
			}else if(!pass.equals(loginpass)){/*@If not equal to the string(password) then sends a message*/
				status.setText("Status:You entered the wrong password.");
			}else{
				status.setText("Status:Error has occured.");
			}
		}	
	}
}
	


